Structures and Functions
Example Test yourself
w TASK: The program discussed shows 2 functions: *new() and new1 that create a new object for the structure
struct student{
char name[30];
int StudentID;
}
struct student *new(){
struct student *aux;
aux = new student;
if (aux == NULL)
cout << “1: Out of memory";
return (aux);
};
void new1(struct student **John){
struct student *aux;
aux = new student;
if (aux == NULL)
cout << “2: Out of memory";
else *John = aux;
};
int main(){
struct student John, *pJohn,
*pJohn1, *new();
// allocate memory for pJohn using new
pJohn = new();
// allocate memory for pJohn1 using new1
new1(&pJohn1);
// now all variables have memory allocated
//and component access is well defined.
John.StudentID = 812366;
pJohn->StudentID = John.StudentID+1;
pJohn1->StudentID = 877744;
….
};